package kpi.java_training.domain.questions;

import kpi.java_training.utils.Parser;
import kpi.java_training.utils.XmlConstants;

import java.util.Arrays;
import java.util.List;

public class MultiAnswersQuestion extends Question {
    Parser parser;

    public MultiAnswersQuestion(String textOfQuestion, List<String> answerVariants, String correctAnswer, int complexity) {
        super (textOfQuestion, answerVariants, correctAnswer, complexity);
        parser = new Parser();
    }

    @Override
    protected String getExplanationText() {
        return "Choose several answers, enter separated by comas, in any order";
    }

    @Override
    public boolean parseUserAnswer(String userAnswer) {
        boolean result = false;
        String[] userAnswers = parser.getParsedString(userAnswer, ",");
        String[] correctAnswers = parser.getParsedString(correctAnswer, ",");

        Arrays.sort(userAnswers);
        Arrays.sort(correctAnswers);
        if (correctAnswers.length == userAnswers.length) {
            for (int i = 0; i < correctAnswers.length; i++) {
                if (userAnswers[i].equals(correctAnswers[i])) {
                    result = true;
                }
            }
        }
        return result;
    }

    @Override
    protected String getType() {
        return XmlConstants.QUESTION_TYPE_MULTI;
    }
}
